$(function(){
	
	$("#loginForm").submit(function(){
		var login = $("#loginDivLogin").val().toLowerCase().trim();
		var pwd = $("#loginDivPassword").val().trim();
		var hash = hex_md5(login+pwd);
		$("#loginDivPasswordHidden").val(hash);
		var profit = false;
		
		$.ajax({
			url: '/users/login',
			type: "POST",
			data: {wl: login, wh: hash},
			async: false,
			success: function(data){
				console.log(data);
				if (data == '1')
				{
					$("#loginDivPassword").val('');
					profit = true;
				}
				else
				{
					$("#loginResponse").html("Неверные логин или пароль");
				}
			}
		});

		if (!profit)
			return false;
	});
	
	$("#logoutLink").click(function(){
		if ($.browser.msie)
		{
			location.href = '/users/logout?msie';
		}
			else
		{
			$.get('/users/logout', function(data){
				location.reload();
			})
		}
	});

	$('#searchSubmit').click(function()
	{
		goSearch($("#searchInput").val());
	})

	$('#searchInput').keyup(function(e)
	{
		if (e.keyCode == 13)
			goSearch($("#searchInput").val());
	})

	$("#newCommentArea").keyup(function(e)
	{
		if (e.ctrlKey && e.keyCode == 13)
		{
			$("#newCommentButton").click();
		}
	})

	$("#newCommentButton").click(function()
	{
		var text = $("#newCommentArea").val();
		if (text.length < 2)
			return false;

		$("#newCommentButton").attr('disabled', true).css('background-image', 'url(/img/jcrop.gif)');

		$.ajax({
			url: '/comment/save',
			type: "POST",
			data: {
				page: pageId,
				text: text,
				comment: comment.currentCommentId,
			},
			async: false,
			success: function(data){
				$("#newCommentArea").val('');
				if (data != null)
				{
					var newComment = $(data);
					comment.bindEvents(newComment);
					$('div.subComments[data-parent='+comment.currentCommentId+']').first().append(newComment);
					comment.toOriginal();
				}
				$("#newCommentButton").attr('disabled', false).css('background-image', 'none');
			}
		});
	})

	comment.bindEvents($('#commentsDiv'));

	$('#newCommentCancelButton').click(function()
	{
		comment.toOriginal();
	})

	$.get(base_url+'index/getState');

});

var comment = {

	currentCommentId: 0,

	toOriginal: function()
	{
		$('#newComment').appendTo('#commentsDiv');
		$("#newCommentArea").val('');
		this.currentCommentId = 0;
	},

	bindEvents: function(parent)
	{
		parent.find('.manageComment_reply').click(function()
		{
			var oneCommentDiv = $(this).closest('.commentDiv');
			$('#newComment').appendTo(oneCommentDiv);
			comment.currentCommentId = oneCommentDiv.data('id');
		});

		parent.find('.manageComment_remove').click(function()
		{
			var oneCommentDiv = $(this).closest('.commentDiv');
			$.get(base_url+'comment/delete/'+oneCommentDiv.data('id'), function(data)
			{
				if (data == '1')
				{
					oneCommentDiv.closest('.oneComment').remove();
					comment.currentCommentId = 0;
				}
				else
				{
					// console.log(oneCommentDiv.find('.commentResponse'));
					oneCommentDiv.find('.commentResponse').html(data);
				}
			})
		});
	}

}


function goSearch(val)
{
	val = encodeURIComponent(val.toLowerCase());
	if (val.length > 2)
		location.href = base_url+'search/'+val;
}